package com.dotflat.check.service.impl;

import com.alibaba.fastjson.JSON;
import com.dotflat.check.domain.Checker;
import com.dotflat.check.service.CheckerService;
import com.dotflat.check.service.TianBoCheckerService;
import com.dotflat.check.service.TianBoMqttService;
import com.dotflat.util.excelUtil.StringUtils;
import com.matt.commons.exception.BusinessException;
import com.matt.foundation.model.ErrorCode;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TianBoCheckerServiceImpl implements TianBoCheckerService {

    @Autowired
    private CheckerService checkerService;

    @Autowired
    private TianBoMqttService mqttService;


    @Value("${iot.checker.tianbo.mqtt.topicprefix}")
    public String mqtt_topic_prefix;

    @Value("${iot.checker.tianbo.mqtt.notifybase}")
    public String mqtt_notify_base;

    public String getTopicNameForDevice(String devSno) {
        String mqttTopic = mqtt_topic_prefix + "_" + devSno;

        return mqttTopic;
    }

    public boolean notifyOpenDoor(String areaId, String checkerId) {
        Checker checker = checkerService.getId(checkerId);

        if (checker == null && StringUtils.isBlank(checker.getDevSno())) {
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "考勤机不存在");
        }

        if(StringUtils.isNotEmpty(areaId) && !areaId.equals(checker.getAreaId())){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "考勤机不存在");
        }

        String topic = getTopicNameForDevice(checker.getDevSno());


        String openDoorJSON = "{" +
                "\"method\": \"open_door\"," +
                "\"data\": {}," +
                "\"notify\": \"" + mqtt_notify_base + "/api/iot/tianbo/device/notify\"," +
                "\"params\": {}" +
                "}";

        try {
            mqttService.sendMessage(topic, openDoorJSON);

            return true;
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return false;
    }


    public boolean notifyRemoveAllPerson(String areaId, String checkerId) {
        Checker checker = checkerService.getId(checkerId);

        if (checker == null && StringUtils.isBlank(checker.getDevSno())) {
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "考勤机不存在");
        }

        if(StringUtils.isNotEmpty(areaId) && !areaId.equals(checker.getAreaId())){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "考勤机不存在");
        }

        String topic = getTopicNameForDevice(checker.getDevSno());

        String openDoorJSON = "{" +
                "\"method\": \"delete_all\"," +
                "\"data\": {}," +
                "\"notify\": \"" + mqtt_notify_base + "/api/iot/tianbo/device/notify\"," +
                "\"params\": {}" +
                "}";

        try {
            mqttService.sendMessage(topic, openDoorJSON);

            return true;
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return false;
    }


    public boolean notifySyncPerson(String areaId, String checkerId, List<String> personIds) {
        Checker checker = checkerService.getId(checkerId);

        if (checker == null && StringUtils.isBlank(checker.getDevSno())) {
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "考勤机不存在");
        }

        if(StringUtils.isNotEmpty(areaId) && !areaId.equals(checker.getAreaId())){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "考勤机不存在");
        }

        String topic = getTopicNameForDevice(checker.getDevSno());


        String openDoorJSON = "{" +
                "\"method\": \"sync_person\"," +
                "\"data\": {" +
                "\"path\": \""+ mqtt_notify_base +"/api/iot/tianbo/device/sync_person\"," +
                "\"path_params\": {" +
                "\"dev_sno\": \""+ checker.getDevSno() +"\"," +
                "\"limit\": 10," +
                "\"offset\": 0," +
                "\"total\": "+personIds.size()+"," +
                "\"person_list\": " + JSON.toJSONString(personIds)+ "," +
                "\"person_type\": \"1\"" +
                "}" +
                "}," +
                "\"notify\": \"" + mqtt_notify_base + "/api/iot/tianbo/device/notify\"," +
                "\"params\": {}" +
                "}";

        try {
            mqttService.sendMessage(topic, openDoorJSON);

            return true;
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return false;
    }

}
